package com.net.oil.news;


import android.os.Bundle;
import
androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseFragment;
import com.net.oil.news.adapter.NewsListAdapter;
import com.net.oil.news.bean.NewsListBean;
import com.net.oil.news.contract.NewsListContract;
import com.net.oil.news.presenter.NewsListPresenter;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 新闻列表fragment，
 */
public class NewsListFragment extends BaseFragment<NewsListPresenter> implements NewsListContract.View {
    @BindView(R.id.news_recycler)
    RecyclerView newsRecycler;
    Unbinder unbinder;

    private int categoryId = 0;     //新闻分类id
    private int page = 1;           //当前页
    private int rows = 20;          //分页大小
    private boolean haveMore = true;

    private NewsListAdapter adapter;

    public static NewsListFragment newInstance(int categoryId) {
        NewsListFragment fragment = new NewsListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(Constant.NEWS_TYPE_ID, categoryId);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        presenter = new NewsListPresenter(this);
        Bundle bundle = getArguments();
        if (bundle != null) {
            categoryId = bundle.getInt(Constant.NEWS_TYPE_ID);
        }
        initView();
        initData();
        return view;
    }

    private void initData() {
        presenter.getNewsList(categoryId, page, rows);
    }

    private void initView() {
        adapter = new NewsListAdapter(getContext());
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
//        newsRecycler.addItemDecoration(new DividerItemDecoration(getContext(), layoutManager.getOrientation()));
        newsRecycler.setLayoutManager(layoutManager);
        newsRecycler.setAdapter(adapter);

        newsRecycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //如果不能向上滚动，有更多则加载更多
                if (!newsRecycler.canScrollVertically(1) && haveMore && page > 1) {
                    adapter.changeMoreStatus(Constant.LOADING_MORE);
                    presenter.getNewsList(categoryId, page, rows);
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     * 获取新闻列表成功
     *
     * @param newsListBean 返回的数据
     */
    @Override
    public void getNewsListSuccess(NewsListBean newsListBean) {
        if (newsListBean != null) {
            if (newsListBean.getList() != null && newsListBean.getList().size() > 0) {
                if (page == 1) {
                    adapter.updateData(newsListBean.getList());
                } else if (page > 1) {
                    adapter.loadMore(newsListBean.getList());
                }

                adapter.changeMoreStatus(Constant.PULL_LOAD_MORE);
                haveMore = true;
                page++;
            } else {
                adapter.changeMoreStatus(Constant.LOADING_NONE);
                haveMore = false;
            }

        }

//        if (newsListBean != null && newsListBean.getPaging() != null){
//            if (newsListBean.getPaging().getTotal() == adapter.getItemCount() -1){
//                adapter.changeMoreStatus(Constant.LOADING_NONE);
//                haveMore = false;
//            }else {
//                adapter.changeMoreStatus(Constant.PULL_LOAD_MORE);
//                haveMore = true;
//                page++;
//            }
//        }

    }

    /**
     * 获取新闻列表失败
     */
    @Override
    public void getNewsListFailed() {

    }
}
